from django.forms import ModelForm
from apps.minapp import models
from utils.tencent.cos import upload_file
from django.db.models.fields.files import FieldFile
import time


class CoverAddsForm(ModelForm):
    class Meta:
        model = models.Cover
        fields = "__all__"

    def clean(self):
        cleaned_data = self.cleaned_data
        #  上传文件
        cover_file_object = cleaned_data.get('src')
        print(cover_file_object)
        if not cover_file_object or isinstance(cover_file_object, FieldFile):
            return cleaned_data
        ext = cover_file_object.name.rsplit('.', maxsplit=1)[-1]
        file_name = 'coversrc/' + "{0}.{1}".format(str(int(time.time() * 1000)), ext)
        # print(cover_file_object)
        cleaned_data['src'] = upload_file(cover_file_object, file_name)
        return cleaned_data
